package com.example.bleuesprit.parkAndroidClient.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import org.json.JSONException;
import org.json.JSONObject;



/**
 * Created by bleuesprit on 2015/11/7.
 */
public class parkdbHelper extends SQLiteOpenHelper {


    final String CREATE_PARKSTAB =
            "create table parkstab(" +
                    "uuid int " +
                    "name char(20) not null," +
                    "totalSpace int not null," +
                    "telNumber char(20)," +
                    "emptySpace int not null," +
                    "description text," +
                    "address text," +
                    "longitude char(20) not null," +
                    "latitude char(20) not null" +
                    ");";

    SQLiteDatabase db = null;


    public parkdbHelper(Context context) {
        super(context, "park.db3", null, 1);
        db = getReadableDatabase();

    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_PARKSTAB);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


    public synchronized void   insertPark(JSONObject json) throws JSONException {


        db.execSQL("insert into parkstab (uuid, name ,  description , address , longitude , latitude ) values (?,?,?,?,?,? ),",
                new Object[]{
                        Integer.valueOf(json.getString("uuid")),
                        json.getString("name"),
                        json.getString("description"),
                        json.getString("address"),
                        json.getString("longitude"),
                        json.getString("latitude")});

    }

    public synchronized Cursor queryPark(int parkid)
    {

        return db.rawQuery("select * from parkstab where uuid ==?",new String[]{Integer.toString(parkid)});

    }

    public void closeDB()
    {
        if(db.isOpen())
        {
            db.close();
        }
    }
}
